/*
 * Copyright (c) 2023 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include <mc/log.h>

#include "interface/network_port/fibrechannel.h"

namespace dev {

bool NetworkPort_FibreChannel::update_fcinfo_by_pldm(uint8_t port_id) {
    emulex_fru_info_t fruinfo;
    bool ret = m_pldm_over_mctp_emulex->get_emulex_fru_info(&fruinfo);
    if (!ret) {
        ilog("Error: Failed to update FCInfo");
        return false;
    }
    if(port_id == 0){
        WWNN.set_value(fruinfo.port0_wwnn);
        WWPN.set_value(fruinfo.port0_wwpn);
    }else if(port_id == 1){
        WWNN.set_value(fruinfo.port1_wwnn);
        WWPN.set_value(fruinfo.port1_wwpn);
    }else{
        ilog("Error: Failed to update FCInfo,invaild port id");
        return false;
    }
    
    return true;
}

void NetworkPort_FibreChannel::start_pldm_update_task_emulex(pldm_over_mctp_emulex_ptr pldm_over_mctp,
                                                         mc::milliseconds      interval,
                                                         uint8_t               port_id) {
    m_pldm_over_mctp_emulex = pldm_over_mctp;
    m_pldm_timer            = new mc::core::timer(this);
    m_pldm_timer->timeout.connect([this, port_id]() {
        update_fcinfo_by_pldm(port_id);
    });
    m_pldm_timer->set_single_shot(false);
    m_pldm_timer->start(interval);
}

void NetworkPort_FibreChannel::stop_pldm_update_task() {
    if (m_pldm_timer) {
        m_pldm_timer->stop();
    }
    if (m_pldm_over_mctp_emulex) {
        m_pldm_over_mctp_emulex = nullptr;
    }
}

} // namespace dev

MC_REFLECT(dev::NetworkPort_FibreChannel)